<?php

namespace App\Models\CarModels;

use App\Models\Api\Files;
use App\Models\SystemManagement\Department;
use App\Models\SystemManagement\Employee;
use Illuminate\Database\Eloquent\Model;

class CarInfo extends Model
{
    /**
     *
     */
    protected $table = 'cars';
    protected $fillable = ['number','brand','plates_number','card_time','format','department_id','kinds_id',
        'areas_id','desc','invoice_id','quality_id','vehicle_license_id','registration_id','maintenance_id','framenumber'
        ];


    public function kind()
    {
        return $this->belongsTo(CarComponent::class,'kinds_id')->with('kind');
    }

    public function invoiceImg()
    {
        return $this->hasMany(CarInvoiceFile::class,'f_id')->with('files');
    }

    public function qualityImg()
    {
        return $this->hasMany(CarQualityFile::class,'f_id')->with('files');
    }
    public function vehicleImg()
    {
        return $this->hasMany(CarVehicleLicenseFile::class,'f_id')->with('files');
    }
    public function registrationImg()
    {
        return $this->hasMany(CarRegistrationFile::class,'f_id')->with('files');
    }
    public function maintenanceImg()
    {
        return $this->hasMany(CarMaintenanceFile::class,'f_id')->with('files');
    }
    public function maintenance_records()
    {
        return $this->hasMany(CarMaintence::class,'car_id','id');
    }

    public function department()
    {
        return $this->belongsTo(Department::class,'department_id','id');
    }
    public function use_department()
    {
        return $this->belongsTo(Department::class,'use_department_id','id');
    }

    public function kinds()
    {
        return $this->belongsTo(CarComponent::class,'kinds_id','id')->with('kind');
    }

    public function area()
    {
        return $this->belongsTo(CarArea::class,'areas_id','id')->with('city');
    }

    public function user()
    {
        return $this->belongsTo(Employee::class,'car_user','id')->with('department');
    }

    public function manager()
    {
        return $this->belongsTo(Employee::class,'car_manager','id')->with('department');
    }

    public function maintence()
    {
        return $this->hasMany(CarMaintence::class,'car_id','id');
    }

    public function repair()
    {
        return $this->hasMany(CarRepairRecord::class,'car_id','id')->with('cars')->with('record');
    }

    public function insurance()
    {
        return $this->hasMany(CarInsuranceRecord::class,'car_id','id');
    }

    public function push()
    {
        return $this->hasMany(CarPunishRecord::class,'car_id','id')->with('staff')->with('record');
    }

    public function accident()
    {
        return $this->hasMany(CarAccidentRecord::class,'car_id','id')->with(['staff','detail','record']);
    }

    public function fuel()
    {
        return $this->hasMany(CarFuelRecord::class,'car_id','id')->with('staff');
    }
    public function year()
    {
        return $this->hasMany(CarYearCheckRecord::class,'car_id','id')->with('car')->with('record');
    }

    public function equipment(){
        return $this->hasMany(CarEquipment::class,'car_id')->with('kind');
    }


    public function rent(){
        return $this->hasMany(CarRent::class,'car_id')->with('department')->with('user')->where('status','2');
    }

    public function allocate(){
        return $this->hasMany(CarRent::class,'car_id')->with('department')->with('user')->where('status','1');
    }

    public function check(){
        return $this->hasOne(CarCheck::class,'car_id');
    }
}
